package com.misty.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.misty.blog.bean.param.guestbook.PubliashGKParam;
import com.misty.blog.service.GuestbookService;
import com.misty.common.bean.Result;
import com.misty.common.constants.ResultEnum;
import com.misty.dao.domain.Guestbook;
import com.misty.dao.mapper.GuestbookMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 留言表 服务实现类
 * </p>
 *
 * @author cthulhu
 * @since 2022-12-15
 */
@Service
public class GuestbookServiceImpl extends ServiceImpl<GuestbookMapper, Guestbook> implements GuestbookService {

    @Resource
    private GuestbookMapper guestbookMapper;

     /**
      * @param
      * @return
      * @Description 分页查询反馈
      * @Date 2022/12/16 14:57
      */
    @Override
    public Result<?> page(Integer pageNum, Integer pageSize, String type) {
        LambdaQueryWrapper<Guestbook> queryWrapper = Wrappers.<Guestbook>lambdaQuery();
        queryWrapper.orderByAsc(type.equals("hot"),Guestbook::getLikesCount,Guestbook::getLikesCount);
        queryWrapper.orderByDesc(type.equals("new"),Guestbook::getUpdateTime);

        Page<Guestbook> guestbookPageage =  guestbookMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        if(guestbookPageage.getTotal() <= 0) {
            return Result.error("分页查询博客失败！");
        }
        return Result.success("分页查询反馈成功!",guestbookPageage);
    }

     /**
      * @param	null
      * @return
      * @Description  发布反馈
      * @Date 2022/12/16 16:53
      */
    @Override
    public Result<?> publishGuestbook(PubliashGKParam param) {
        Guestbook guestbook = new Guestbook();
        guestbook.setUserId(param.getUserId())
                .setContent(param.getContent())
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        int insert = guestbookMapper.insert(guestbook);
        if(insert <= 0) {
            return Result.error("发布反馈失败!");
        }
        return Result.success("发布反馈成功!");
    }
}
